package cn.learn.office.word;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 读取word文档
 *
 * @author huangyezhan
 * @version 1.0
 * @date 2021年01月26日17:09
 */
@Slf4j
public class WordReader {

    private static final String WORD  = ".doc";
    private static final String word_ = ".docx";

    public String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(WORD)) {
                InputStream   is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
                ex.close();
            } else if (path.endsWith(word_)) {
                InputStream is = new FileInputStream(new File(path));
                XWPFDocument document = new XWPFDocument(is);
                //OPCPackage          opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor  = new XWPFWordExtractor(document);
                buffer = extractor.getText();
                extractor.close();
            } else {
                System.out.println("此文件不是word文件！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return buffer;
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        WordReader tp = new WordReader();
        String content = tp.readWord("E:\\download\\爬虫.docx");
        //System.out.println("content===="+content);

        //InputStream is2 = new FileInputStream("E:\\download\\一户式说明.doc");

        //tp.read2(is2);
    }


    public void read2(InputStream is){
        WordExtractor extractor = null;
        try {
            extractor = new WordExtractor(is);

            //输出word文档所有的文本
            System.out.println(extractor.getText());

            System.out.println(extractor.getTextFromPieces());
            System.out.println(extractor.getMainTextboxText().toString());
            //输出页眉的内容
            System.out.println("页眉：" + extractor.getHeaderText());
            //输出页脚的内容
            System.out.println("页脚：" + extractor.getFooterText());
            //输出当前word文档的元数据信息，包括作者、文档的修改时间等。
            System.out.println(extractor.getMetadataTextExtractor().getText());

            //获取各个段落的文本
            String[] paraTexts = extractor.getParagraphText();
            for (int i=0; i<paraTexts.length; i++) {
                System.out.println("Paragraph " + (i+1) + " : " + paraTexts[i]);
            }
            //输出当前word的一些信息
            printInfo(extractor.getSummaryInformation());
            //输出当前word的一些信息
            this.printInfo(extractor.getDocSummaryInformation());
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void printInfo(SummaryInformation info) {
        //作者
        System.out.println(info.getAuthor());
        //字符统计
        System.out.println(info.getCharCount());
        //页数
        System.out.println(info.getPageCount());
        //标题
        System.out.println(info.getTitle());
        //主题
        System.out.println(info.getSubject());
    }
    private void printInfo(DocumentSummaryInformation info) {
        //分类
        System.out.println(info.getCategory());
        //公司
        System.out.println(info.getCompany());
    }



}
